//
//  CGGRectangle.c
//  PictureYourself
//
//  Created by Daniel on 4/8/12.
//
//

#include "CGGRectangle.h"

CGMutablePathRef CreateRoundRectPath(CGContextRef context, CGRect bounds)
{
    CGContextSetLineWidth(context, 0.5);
    CGFloat x_origin = bounds.origin.x;
    CGFloat y_origin = bounds.origin.y;
    CGFloat width = bounds.size.width;
    CGFloat height = bounds.size.height;
    CGFloat averageSize = (bounds.size.width + bounds.size.height) / 2;
    CGFloat cornerRadius = averageSize * 0.1;
    
    CGMutablePathRef path = CGPathCreateMutable();
    
    //CGContextBeginPath(context);
    CGPathMoveToPoint(path, NULL, x_origin, y_origin + cornerRadius);
    
    CGPathAddArcToPoint(path, NULL, x_origin, y_origin, x_origin + cornerRadius, y_origin, cornerRadius);
    CGPathAddLineToPoint(path, NULL, x_origin + width - cornerRadius, y_origin);
    
    CGPathAddArcToPoint(path, NULL, x_origin + width, y_origin, x_origin + width, y_origin + cornerRadius, cornerRadius);
    CGPathAddLineToPoint(path, NULL, x_origin + width, y_origin + height - cornerRadius);
    
    CGPathAddArcToPoint(path, NULL, x_origin + width, y_origin + height, x_origin + width - cornerRadius, y_origin + height, cornerRadius);
    CGPathAddLineToPoint(path, NULL, x_origin + cornerRadius, y_origin + height);
    
    CGPathAddArcToPoint(path, NULL, x_origin, y_origin + height, x_origin, y_origin + height - cornerRadius, cornerRadius);
    CGPathAddLineToPoint(path, NULL, x_origin , y_origin + cornerRadius);
    
    CGPathCloseSubpath(path);
    
    CGContextAddPath(context, path);
    
    return path;
}

void DrawInnerShadow(CGContextRef context, CGPathRef path, CGFloat shadowBaseSize)
{
    CGContextSetLineWidth(context, shadowBaseSize);
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    CGFloat blur = shadowBaseSize / 3.0;
    
    CGContextAddPath(context, path);
    CGContextSetShadow(context, CGSizeMake(0.0, shadowBaseSize), blur);
    CGContextStrokePath(context);

    CGContextAddPath(context, path);
    CGContextSetShadow(context, CGSizeMake(shadowBaseSize, 0.0), blur);
    CGContextStrokePath(context);
    
    CGContextAddPath(context, path);
    CGContextSetShadow(context, CGSizeMake(-(shadowBaseSize), 0.0), blur);
    CGContextStrokePath(context);
}